<?php

include_once '../Interfaces/XMLInterface.php';
include_once '../Exceptions/CommunicationException.php';
include_once '../Model/GPS.php';
include_once '../InfoManager/InfoZtl.php';

/**
 * 
 * @author Davide Lombardi
 *
 */
class GateZtl implements XMLInterface {
	
	private $gatename=null;
	private $gateID=null;
	private $via=null;
	private $tipo=null;
	private $zona=null;
	
	private $gps=null;
	private $distance=null;
	private $elementStart=null;
	private $elementEnd=null;
		
	private $timestart1=null;
	private $timeend1=null;
	private $timestart2=null;
	private $timeend2=null;
	private $timestart3=null;
	private $timeend3=null;
	
	private $start1=null;
	private $end1=null;
	private $start2=null;
	private $end2=null;
	private $start3=null;
	private $end3=null;
	
	private $lun_gio=null;
	private $ven=null;
	private $sab=null;
	private $dom=null;
	
	private $lat=null;
	private $long=null;
	
	private $stato=null;
	private $indice=null;

	function __construct($gateID, $via, $zona, $tipo, $lat, $long, $lun_gio, $ven, $sab, $dom, $stato){
		$this->gateID=$gateID;
		$this->via=$via;
		$this->zona=$zona;
		$this->tipo=$tipo;
		$this->lat=$lat;
		$this->long=$long;
		//$this->timestart1=$timestart1;
		//$this->timeend1=$timeend1;
		//$this->timestart2=$timestart2;
		//$this->timeend2=$timeend2;
		//$this->timestart3=$timestart3;
		//$this->timeend3=$timeend3;
		$this->lun_gio=$lun_gio;
		$this->ven=$ven;
		$this->sab=$sab;
		$this->dom=$dom;
		$this->stato=$stato;
	
		if (($lat!=null) && ($long!=null)){
				$this->gps = new GPS($lat,$long);
			}
			
	//	if ($this->via==null)
	//	$this->fillZtlGate();
	
			
		$this->elementStart = "<GATE>";
		$this->elementEnd = "</GATE>";
	
	}
	
	private function fillZtlGate() {
		//Recupera le coordinate della stazione ed eventualmente il nome dal db
		$im = new InfoZtl();
		$info = $im->getGateInformation($this->via);
		
		//Potremmo controllare se info � stringa vuota
		
		if ($info!="")
		{
			$parts = explode(InfoZtl::$INFOSEPARATOR, $info);
			
			
			$this->via = $parts[2];
			$this->gps = new GPS($parts[5],$parts[6]);
		}
		
		unset($im);
		unset($info);
	}
	
	/**
	Metodi di set() e get() per i parametri
	*/
	
	function getGatename(){
		return $this->via;
	}
	
	function getGateID(){
		return $this->gateID;
	}
	
	function getVia(){
		return $this->via;
	}
	
	function getTipo(){
		return $this->tipo;
	}
	
	function getPosition(){
		return $this->gps;
	}
	
	function getZona(){
		return $this->zona;
	}
	
	function getDistance() {		
		return $this->distance;		
	}

	function setDistance($distance) {
		$this->distance = $distance;	
	}
	
	function getStato(){
		return $this->stato;
	}
	
	function getLat(){
		return $this->lat;
	}
	function getLon(){
		return $this->long;
	}
	function setIndice($i){
		$this->indice = $i;
	}
	
	function setStato ($t1,$e1,$t2,$e2,$t3,$e3){
		$statogate=null;
		$this->$start1=$t1;$this->$end1=$e1;
		$this->$start2=$t2;$this->$end2=$e2;
		$this->$start3=$t3;$this->$end3=$e3;
		$statogate = TimeControl(start1,end1,start2,end2,start3,end3);
		$this->stato= $statogate;
	}
	
/**
	 * Sintetizza l'oggetto in xml. Lancia un'eccezione di tipo CommunicationException
	 * in caso di problemi di comunicazione con Atac
	 * @see src/Interfaces/XMLInterface#toXML()
	 */
	function toXML($mode) {
		
		$xml = "\n";
		$xml = $xml."<GATEID>".$this->gateID."</GATEID>\n";
		$xml = $xml."<GATENAME>".$this->gatename."</GATENAME>\n";
		$xml = $xml."<ADDRESS>".$this->via."</ADDRESS>\n";
		$xml = $xml."<ZONA>".$this->zona."</ZONA>\n";
		$xml = $xml."<TIPO>".$this->tipo."</TIPO>\n";
		/*
		$xml = $xml."<START1>".$this->timestart1."</START1>\n";
		$xml = $xml."<END1>".$this->timeend1."</END1>\n";
		$xml = $xml."<START2>".$this->timestart2."</START2>\n";
		$xml = $xml."<END2>".$this->timeend2."</END2>\n";
		$xml = $xml."<START3>".$this->timestart3."</START3>\n";	
		$xml = $xml."<END3>".$this->timeend3."</END3>\n";
		*/
		$xml = $xml."<LUN-GIO>".$this->lun_gio."</LUN-GIO>\n";
		$xml = $xml."<VEN>".$this->ven."</VEN>\n";
		$xml = $xml."<SAB>".$this->sab."</SAB>\n";
		$xml = $xml."<DOM>".$this->dom."</DOM>\n";
		$xml = $xml."<DISTANCE>".$this->distance."</DISTANCE>\n";
		$xml = $xml."<STATO>".$this->stato."</STATO>\n";
		$xml = $xml."<INDICE>".$this->indice."</INDICE>\n";
		
		if ($this->gps!=null)
		{
			$xml = $xml."<POS>\n";
			$xml = $xml."<LAT>".$this->gps->getLat()."</LAT>\n";
			$xml = $xml."<LONG>".$this->gps->getLon()."</LONG>\n";
			$xml = $xml."</POS>\n";
		}
		else
		{
			$xml = $xml."<POS>\n";
			$xml = $xml."<LAT></LAT>\n";
			$xml = $xml."<LONG></LONG>\n";
			$xml = $xml."</POS>\n";
			
		}
		/*	
		if ($mode == EXTENDED_MODE) {
			$xml = $xml."<STATO>".$this->stato."</STATO>\n";
		}
		*/
		$xml = "\n".$this->elementStart."\n".$xml.$this->elementEnd."\n";
		return $xml;
	}

	/**
	 * (non-PHPdoc)
	 * @see src/Interfaces/XMLInterface#getElementStartTag()
	 */
	function getElementStartTag() {
		return $this->elementStart;	
	}
	
	/**
	 * (non-PHPdoc)
	 * @see src/Interfaces/XMLInterface#getElementEndTag()
	 */
	function getElementEndTag() {
		return $this->elementEnd;	
	}

}

?>